<?php
	class deletedmodel extends Model {
		function deletedmodel()
		{
			parent::Model();			
		}
		
		function countAll() {
			$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source
				FROM student
				where is_active != 1
				union 
				SELECT maching_id, maching_date , note, 'หน้าจับคู่ครูกับนร.' as source
				FROM tutorland_maching 
				where status = 'reject' ";
			$query = $this->db->query($sql);  
			$count = $query->num_rows(); 
	  		return $count;
		}
		
		function selectAll($limit = NULL, $offset = NULL)
		{
			$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source, update_by
				FROM student
				where is_active != 1
				union 
				SELECT maching_id, approve_date , note, 'หน้าจับคู่ครูกับนร.' as source, approve_by
				FROM tutorland_maching 
				where status = 'reject'
				order by source, update_date desc limit ";
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$query = $this->db->query($sql); 
			return $query->result();
		}
		
		function countCriteria($data) {
			if($data['source'] == "หน้าจับคู่ครูกับนร.") {
				$sql = "
					SELECT maching_id, maching_date , note, 'หน้าจับคู่ครูกับนร.' as source, approve_by
					FROM tutorland_maching 
					where status = 'reject' ";
				if($data['deleted_by'] != null ) {
					$sql .= " and approve_by = '". $data['deleted_by'] . "' ";
				}
				$query = $this->db->query($sql);  
				$count = $query->num_rows(); 
			} else if($data['source'] == "รายชื่อ นร. สมัครผ่านเว็บ") {
				$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source, update_by
					FROM student
					where is_active != 1 ";
				if($data['deleted_by'] != null ) {
					$sql .= " and update_by = '". $data['deleted_by'] . "' ";
				}
				$query = $this->db->query($sql);  
				$count = $query->num_rows();
			} else {
				$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source, update_by
					FROM student
					where is_active != 1 ";
				if($data['deleted_by'] != null ) {
					$sql .= " and update_by = '". $data['deleted_by'] . "' ";
				}
					$sql .= " union 
					SELECT maching_id, maching_date , note, 'หน้าจับคู่ครูกับนร.' as source, approve_by
					FROM tutorland_maching 
					where status = 'reject' ";
				if($data['deleted_by'] != null ) {
					$sql .= " and approve_by = '". $data['deleted_by'] . "' ";
				}					
				$query = $this->db->query($sql);  
				$count = $query->num_rows();
			}
	  		return $count;
		}
		
		function selectCriteria($limit = NULL, $offset = NULL,$data)
		{
			if($data['source'] == "หน้าจับคู่ครูกับนร.") {
				$sql = "
					SELECT maching_id as student_id, approve_date  as update_date, note as comment, 'หน้าจับคู่ครูกับนร.' as source, approve_by as update_by  
					FROM tutorland_maching 
					where status = 'reject'";
				if($data['deleted_by'] != null ) {
					$sql .= " and approve_by = '". $data['deleted_by'] . "' ";
				}
					$sql .= " order by source, update_date desc limit ";
			} else if($data['source'] == "รายชื่อ นร. สมัครผ่านเว็บ") {
				$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source, update_by
					FROM student
					where is_active != 1 ";
				if($data['deleted_by'] != null ) {
					$sql .= " and update_by = '". $data['deleted_by'] . "' ";
				}
					$sql .= " order by source, update_date desc limit ";
			} else {
				$sql = "SELECT student_id, update_date, comment, 'รายชื่อ นร. สมัครผ่านเว็บ' as source, update_by
					FROM student
					where is_active != 1 ";
				if($data['deleted_by'] != null ) {
					$sql .= " and update_by = '". $data['deleted_by'] . "' ";
				}
					$sql .= " union 
					SELECT maching_id as student_id, approve_date  as update_date, note as comment, 'หน้าจับคู่ครูกับนร.' as source, approve_by as update_by  
					FROM tutorland_maching 
					where status = 'reject'";
					if($data['deleted_by'] != null ) {
					$sql .= " and approve_by = '". $data['deleted_by'] . "' ";
				}
					$sql .= "
					order by source, update_date desc limit ";
			}
			if($offset != null) {
				$sql .= $offset . ", ";
			}
			$sql .= $limit;
			$query = $this->db->query($sql); 
			return $query->result();
		}
	}
?>